package com.lydms.web;

import com.lydms.common.ApiResult;
import com.lydms.pojo.Page;
import com.lydms.service.LearnSmsService;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 定时发送学习的进度
 *
 * @author
 */
@RestController
@RequestMapping("/learn")
public class LearnSmsController {
    private static final Logger logger = LogManager.getLogger(LearnSmsController.class);

    @Autowired
    private LearnSmsService learnSmsService;

    @Value(value = "${learnSms.phones}")
    private String phones;

    @RequestMapping(value = "/addPlan", method = {RequestMethod.POST})
    public ApiResult addPage(@RequestBody Map<String, Object> params) {
        logger.info("name:"+params.get("name")+"page: "+params.get("page"));

//        1、入参的检测
        String name = null;
        int page = 0;
        if (params.get("name") != null) {
            name = params.get("name").toString();
        }
        if (StringUtils.isEmpty(name)) {
            return ApiResult.fail(105, "请传入姓名");
        }
        if (!("李阳达".equals(name))) {
            if (!("刘金晶".equals(name))) {
                return ApiResult.fail(105, "请传入正确的姓名");
            }
        }
        if (params.get("page") != null) {
            page = Integer.parseInt(params.get("page").toString());
        }
        if (page == 0) {
            return ApiResult.fail(105, "请传入页数");
        }

//        2、参数的封装
        Page pageBean = new Page();
        pageBean.setName(name);
        pageBean.setPage(page);

//        3、调用插入数据的service服务
        int result = learnSmsService.addPage(pageBean);

//        4、返回值判断
        if (result == 1) {
            return ApiResult.succ(pageBean);
        } else {
            return ApiResult.fail(103, "插入数据库失败");
        }
    }


    /**
     * 获取每天的 完成情况
     *
     * @return
     */
    @RequestMapping(value = "/getPlan", method = {RequestMethod.POST})
    public ApiResult getPage() {
//        1、调用service层，获取每天的完成情况
        List<Page> pageList = learnSmsService.getPage();

//        2、返回值判断
        if (pageList.size() > 0) {
            return ApiResult.succ(pageList);
        } else {
            return ApiResult.fail(103, "获取每天的完成情况失败");
        }
    }


    /**
     * 模拟定时短信服务
     * @return
     */
    @RequestMapping(value = "/smsPage", method = {RequestMethod.POST})
    public ApiResult sendSmsPage() {
        List<Page> pageList = learnSmsService.getPage();
        List result = learnSmsService.sendSms(pageList, phones);

        if (result!=null){
            return ApiResult.succ(result);
        }
        return ApiResult.fail(103,"发送短信失败");
    }
}


